import type { VxeGlobalRendererHandles } from 'vxe-table'
import XEUtils from 'xe-utils'
import useGlobalConfig from '../utils/use-global-config'
import { moneyUppercase, isValidVal } from '../utils/utils'

const { YEARMONTH, DATE, DATETIME, LONGDATETIME, TIME, LONGTIME, PRECISION, ROUND, SYMBOL } =
  useGlobalConfig().Format

// 填充字符 fill  长度 length
export const digital = ({ cellValue }, { fill = '0', length = 8 } = {}) => {
  if (!isValidVal(cellValue)) return cellValue
  const number = parseInt(cellValue, 10)
  return (Number.isNaN(number) ? '0' : number.toString()).padStart(length, fill)
}

// 小数位 precision，进位点 round
export const number = ({ cellValue }, { precision = PRECISION, round = ROUND } = {}) => {
  // console.log(cellValue, round, precision)
  if (!isValidVal(cellValue)) return cellValue
  const numStr = XEUtils.toNumberString(cellValue)
  const floatStr = numStr.split('.')[1] || ''
  const flagStr = floatStr.substr(precision, 1)
  if (flagStr >= round) {
    return XEUtils.ceil(cellValue, precision)
  } else {
    return XEUtils.floor(cellValue, precision)
  }
}

// 符号 symbol 小数位 precision, 进位点 round, 现金额转大写 isUppercase
export const money = (
  { cellValue },
  { precision = PRECISION, round = ROUND, symbol = SYMBOL, isUppercase = false } = {}
) => {
  if (!isValidVal(cellValue)) return cellValue
  if (isUppercase) {
    return moneyUppercase(cellValue)
  }
  const num = number({ cellValue }, { precision, round })
  return symbol + XEUtils.commafy(num, { digits: precision })
}

// 单位(TB GB M KB B)
export const filesize = ({ cellValue }) => {
  if (!isValidVal(cellValue)) return cellValue
  const unitArr = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB']
  let index = 0
  const srcsize = parseFloat(cellValue)
  index = Math.floor(Math.log(srcsize) / Math.log(1024))
  const size = srcsize / Math.pow(1024, index)
  const sizeStr = size.toFixed(2) //保留的小数位数
  return sizeStr + unitArr[index]
}

// 是/否
export const boolean = ({ cellValue }) => {
  if (!isValidVal(cellValue)) return cellValue
  return !!cellValue
}

// 年月日
export const date = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || DATE)
}
// 年月日时分
export const dateTime = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || DATETIME)
}
// 年月日时分秒
export const longDateTime = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || LONGDATETIME)
}
// 年月
export const yearMonth = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || YEARMONTH)
}
// 时分
export const time = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || TIME)
}
// 时分秒
export const longTime = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || LONGTIME)
}

// 格式化性别
export const formatSex = ({ cellValue }: any) => {
  return cellValue ? (cellValue === '1' ? '男' : '女') : ''
}
// 格式化下拉选项
export const formatSelect = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  list: Record<string, any>
) => {
  const item = list.find((item: any) => item.value === cellValue)
  return item ? item.label : ''
}
// 格式化日期，默认 yyyy-MM-dd HH:mm:ss
export const formatDateTime = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || LONGDATETIME)
}

export const formatDate = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return XEUtils.toDateString(cellValue, format || DATE)
}
// 四舍五入金额，每隔3位逗号分隔，默认2位数
export const formatAmount = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  digits = 2
) => {
  return XEUtils.commafy(Number(cellValue), { digits })
}
// 格式化银行卡，默认每4位空格隔开
export const formatBankcard = ({ cellValue }: VxeGlobalRendererHandles.FilterMethodParams) => {
  return XEUtils.commafy(XEUtils.toValueString(cellValue), { spaceNumber: 4, separator: ' ' })
}
// 四舍五入,默认两位数
export const formatFixedNumber = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  digits = 2
) => {
  return XEUtils.toFixed(XEUtils.round(cellValue, digits), digits)
}
// 向下舍入,默认两位数
export const formatCutNumber = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  digits = 2
) => {
  return XEUtils.toFixed(XEUtils.floor(cellValue, digits), digits)
}
// 转换 moment 类型为字符串
export const toMomentString = (
  { cellValue }: VxeGlobalRendererHandles.FilterMethodParams,
  format: string
) => {
  return cellValue ? cellValue.format(format) : ''
}
